package com.only4play.practice.task.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.only4play.practice.feign.ReconcileTaskFeign;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author mengdy
 */
@Component
@Slf4j
public class ReconcileTaskHandler {

    @Resource
    private ReconcileTaskFeign reconcileTaskFeign;

    /**
     * 对账任务
     * XxlJob注解 的value值要跟xxl-job-admin任务管理中新增任务时配置的jobHandler名称一致
     */
    @XxlJob(value = "onlineReconcileHandler")
    public void onlineReconcileHandler() throws Exception {
        long jobId = XxlJobHelper.getJobId();
        String jobParam = XxlJobHelper.getJobParam();
        log.info("线上支付对账任务开始执行  jobId:{},jobParam:{}", jobId, jobParam);

        // TODO 调用业务层方法
        if (StrUtil.isNotEmpty(jobParam)){
            JSONObject jsonObject = JSONUtil.parseObj(jobParam);
            // yyyy-MM-dd
            String billDateStr = (String) jsonObject.get("billDateStr");
            reconcileTaskFeign.onlineReconcileTask(billDateStr);
        }else {
            reconcileTaskFeign.onlineReconcileTask(null);
        }
    }
}
